package com.zhongli.devplatform.security.weixin;

import com.alibaba.fastjson.JSON;
import com.zhongli.devplatform.bean.SecurityUser;
import com.zhongli.devplatform.po.sys.ReadUserLoginPO;
import com.zhongli.devplatform.service.imp.sys.ReadUserLoginService;
import com.zhongli.devplatform.service.imp.user.ReadUserService;
import com.zhongli.devplatform.service.imp.sys.ReadSystemParameterService;
import com.zhongli.devplatform.utils.CusAccessObjectUtil;
import com.zhongli.devplatform.component.SpringUtil;
import com.zhongli.devplatform.bean.Res;
import eu.bitwalker.useragentutils.UserAgent;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

public class WeixinAuthenticationSuccessHandler implements AuthenticationSuccessHandler {


    @SneakyThrows
    @Override
    public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
        httpServletResponse.setContentType("application/json;charset=utf-8");

        ReadUserService userService = SpringUtil.getBean(ReadUserService.class);

        ReadUserLoginService userLoginService = SpringUtil.getBean(ReadUserLoginService.class);

        ReadSystemParameterService systemParameterService = SpringUtil.getBean(ReadSystemParameterService.class);

        String weixinId = httpServletRequest.getParameter("weixinId");
        String username = httpServletRequest.getParameter("username");
        if (!StringUtils.isBlank(weixinId)) {

            SecurityUser securityUser = (SecurityUser) authentication.getPrincipal();
            username = securityUser.getUsername();
            if (securityUser.getWeixinId() == null || !securityUser.getWeixinId().equals(weixinId)) {
                userService.updateWeixinId(securityUser.getId(), weixinId);
            }
        }


        ReadUserLoginPO userLoginPO = new ReadUserLoginPO();
        UserAgent userAgent = UserAgent.parseUserAgentString(httpServletRequest.getHeader("User-Agent"));
        userLoginPO.setLoginBrowser(userAgent.getBrowser().getName());
        userAgent.getOperatingSystem().getDeviceType();
        userLoginPO.setLoginIp(CusAccessObjectUtil.getIpAddress(httpServletRequest));
        userLoginPO.setLoginStatus("1");
        userLoginPO.setLoginType(StringUtils.isBlank(weixinId) ? "1" : "2");
        userLoginPO.setLoginUa(httpServletRequest.getHeader("User-Agent"));
        userLoginPO.setLoginTime(new Date());
        userLoginPO.setUserName(username);
        userLoginService.save(userLoginPO);


        String weixin_login_success_type = systemParameterService.getSystemParameter("weixin_login_success_type");
        if ("redirect".equals(weixin_login_success_type)) {
            String weixin_login_success_url = systemParameterService.getSystemParameter("weixin_login_success_url");
            httpServletResponse.sendRedirect(weixin_login_success_url);
        } else {
            PrintWriter out = httpServletResponse.getWriter();
            out.write(JSON.toJSONString(new Res(null)));
            out.flush();
            out.close();
        }


    }
}
